package com.example.pigeonappadminbackend.common.exception;

import com.example.pigeonappadminbackend.common.BaseResponse;
import com.example.pigeonappadminbackend.common.ErrorCode;
import com.example.pigeonappadminbackend.common.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice  // controller增强器，可以全局处理异常
public class GlobalExceptionHandler {

    /**
     * 专门处理自定义业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> handleBusinessException(BusinessException e) {
        log.error("BusinessException: {}", e.getMessage());
        return ResultUtils.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> handleRuntimeException(RuntimeException e) {
        log.error("RuntimeException: {}", e.getMessage());
        return ResultUtils.fail(ErrorCode.SYSTEM_ERROR, "系统异常");
    }
}
